package com.lfd.hashmap;

import java.util.concurrent.CountDownLatch;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.hashmap
 * @文件名: CountDownLatchExample
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/13 16:01
 * @描述：
 */
public class CountDownLatchExample {
    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(1); // 初始化计数器为 1

        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("Thread 1 is running");
            latch.countDown(); // 计数器减 1
        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            try {
                latch.await(); // 等待计数器为 0
                System.out.println("Thread 2 is running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t2.start(); // 启动 t2
        t1.start(); // 启动 t1
    }
}
